package com.stjb.furns.web;

import com.stjb.furns.entity.Furn;
import com.stjb.furns.entity.Page;
import com.stjb.furns.service.FurnsService;
import com.stjb.furns.service.impl.FurnsServiceImpl;
import com.stjb.furns.utils.DataUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author situ
 * @version 1.0
 * @date 2025/5/28 4:44
 * @Description:
 */
public class CustomerFurnServlet extends BasicServlet {

    private FurnsService furnsService = new FurnsServiceImpl();

    /**
     * 顾客首页分页
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pagNo = DataUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = DataUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);

        //调用Servlet方法 获取Page对象
        Page<Furn> page = furnsService.page(pagNo, pageSize);
        // System.out.println(page);
        //将page放入request域
        req.setAttribute("page",page);
        //请求转发
        req.getRequestDispatcher("/views/customer/index.jsp").forward(req,resp);
    }


    /**
     * 首页搜索 和 首页
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void pageByName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pagNo = DataUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = DataUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        String name = req.getParameter("name");
        if (null==name){
            name="";
        }

        //调用Service方法 获取Page对象
        Page<Furn> page = furnsService.pageByName(pagNo, pageSize,name);

        //StringBuilder 内部维护一个可变的字符数组
        StringBuilder url = new StringBuilder("customerFurnServlet?action=pageByName");
        if (!"".equals(name)){
            url.append("&name=").append(name);
        }

        page.setUrl(url.toString());

        // System.out.println(page);
        //将page放入request域
        req.setAttribute("page",page);
        //请求转发
        req.getRequestDispatcher("/views/customer/index.jsp").forward(req,resp);
    }
}
